package ar.com.solidussource.howmuchservices.web.controller;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import ar.com.solidussource.howmuchservices.common.entities.Person;
import ar.com.solidussource.howmuchservices.common.entities.User;

public class BaseController {
	
	protected static String RESPONSE_OK = "done";
	protected static String RESPONSE_NOOK = "system failure";
	protected static String CURRENT_USER = "currentUser";
	protected static String USER_IS_NOT_LOGGED = "user is not logged";
	
	protected User getCurrentUser(){
		//ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
		//return (User) attr.getAttribute(CURRENT_USER, RequestAttributes.SCOPE_SESSION);
		
		Person person = new Person();
		
		person.setId(1);
		person.setName("anonymous");
		person.setLastname("anonymous");
				
		User user = new User();
		user.setId(1);
		user.setPerson(person);
		user.setUsername("anonymous");
		user.setPassword("anonymous");
		
		return user;
	}
	
	protected void setCurrentUser(User user){
		//ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
		//attr.setAttribute(CURRENT_USER, user, RequestAttributes.SCOPE_SESSION);		
	}
	
	protected void cleanCurrentUser(){
		//ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
		//attr.setAttribute(CURRENT_USER, null, RequestAttributes.SCOPE_SESSION);		
	}
	
	protected boolean isUserLogged(){
		ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
		
		User user = (User) attr.getAttribute(CURRENT_USER, RequestAttributes.SCOPE_SESSION);
		if (user != null) {
			if (user.getUsername() != null || !user.getUsername().equals("")) {
				return true;
			} else {
				return false;
			}
			
		} else {
			return false;
		}
	}

	protected String getJsonStatus(String textStatus) {
		return "{\"status\":\""+textStatus+"\"}";
	}

}
